<template>
  <div class="template pad">
    <div class="head">
      <span>AI模板设置</span>
    </div>
    <el-tabs class="m-t-b" type="border-card">
      <el-tab-pane label="添加AI模板">
        <el-form :model="aiTemplate" label-position="top" ref="aiTemplate" :rules="rules">
          <el-form-item label="模板名称" prop="aiTemplateName">
            <el-input v-model="aiTemplate.aiTemplateName" />
          </el-form-item>
          <el-form-item label="模板编号" prop="aiTemplateNo">
            <el-input v-model="aiTemplate.aiTemplateNo" />
          </el-form-item>
          <el-form-item label="模板标椎图片">
            <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              disabled
              drag
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="aiTemplate.imgUrl" :src="aiTemplate.imgUrl" class="avatar">
              <div class="tip-wrap">
                <i class="el-icon-plus avatar-uploader-icon"></i>
                <div class="upload-tip">拖拽/点击上传(当前禁用)</div>
              </div>
              <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" rows="3" v-model="aiTemplate.remark" />
          </el-form-item>
          <el-button type="primary" @click="handleSubmit('aiTemplate')">提 交</el-button>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="添加AI学习图片" disabled>
        <el-upload
          action="https://jsonplaceholder.typicode.com/posts/"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible" size="tiny">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        <el-button style="margin-top: 20px;" type="primary" @click="handleStudy">学 习</el-button>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import { notifySuccess } from '../util'
import {
  addAITemplate
} from '../api'

export default {
  name: 'templat',
  data () {
    return {
      studyImgUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      pageSize: 10,
      total: 0,
      aiTemplate: {
        aiTemplateName: '',
        // imgUrl: '',
        aiTemplateNo: '',
        remark: ''
      },
      rules: {
        aiTemplateName: [
          { required: true, message: '该项不能为空!', trigger: 'blur' }
        ],
        aiTemplateNo: [
          { required: true, message: '该项不能为空!', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '该项不能为空!', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleAvatarSuccess (res, file) {
      this.template.imgUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isType = isJPG || isPNG
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isType) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isType && isLt2M
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleStudy () {
      console.log(1)
    },
    handleSubmit (form) {
      if (!this.$checkAuth('canAddAITemplate')) {
        return this.$message.error('暂无对应操作权限!')
      };
      this.$refs[form].validate(valid => {
        if (valid) {
          addAITemplate(this.aiTemplate)
          .then(res => {
            if (res.data.code === 1) {
              notifySuccess(this)
            } else {
              this.$message.error(res.data.msg || '网络异常!')
            }
          })
          .catch(e => {
            this.$message.error('网络异常!')
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
  .template {
    .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      width: 358px;
      height: 178px;
      .tip-wrap {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
    }
    .avatar-uploader .el-upload:hover {
      border-color: $main-color;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
    }
    .avatar {
      width: 358px;
      height: 178px;
      display: block;
    }
  }
</style>
